home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / MorphOS / cvs-1.11.2 / source / amiga / time.c < prev   
Encoding:
C/C++ Source or Header  |  2002-11-18  |  5.2 KB  |  232 lines

  1. /*
  2.  * $Id$
  3.  *
  4.  * :ts=4
  5.  *
  6.  * AmigaOS wrapper routines for GNU CVS, using the RoadShow TCP/IP API
  7.  *
  8.  * Written and adapted by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  *                        Jens Langner <Jens.Langner@light-speed.de>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "SDI_compiler.h"
  27.  
  28. #include <proto/utility.h>
  29. #include <proto/dos.h>
  30. #include <proto/exec.h>
  31.  
  32. #include <stdio.h>
  33. #include <time.h>
  34.  
  35. /* includes only for SASC or which SASC don`t like */
  36. #if defined(__SASC)
  37.   #include <dos.h>
  38. #endif
  39.  
  40. /****************************************************************************/
  41.  
  42. /*#define DEBUG*/
  43. #include "_assert.h"
  44.  
  45. /****************************************************************************/
  46.  
  47. #define UNIX_TIME_OFFSET 252460800
  48.  
  49. /****************************************************************************/
  50.  
  51. extern int amiga_get_minutes_west(void);
  52.  
  53. /* add some features GNUC doesn`t provide by default
  54.    and take care that the libnix of morphOS supports some of those per default
  55. */
  56. #if defined(__GNUC__)
  57.  
  58.   #if !defined(__MORPHOS__)
  59.     STRPTR _ProgramName;
  60.     #define chkabort() __chkabort()
  61.   #endif
  62.  
  63.   /* libnix catches the WB startup code for us, so we don`t need to worry */
  64.   extern struct WBStartup *_WBenchMsg;
  65.   #define WBenchMsg _WBenchMsg
  66.  
  67.   /* prototype for the chkabort() function (libnix supports it) */
  68.   extern void chkabort();
  69. #endif
  70.  
  71. /******************************************************************************/
  72.  
  73. /* This file contains replacements for the SAS/C run time library, with
  74.  * respect to time and date management. They are in a separate file so that
  75.  * they may be compiled with the params=both option, thus really replacing
  76.  * the library routines of the same names.
  77.  */
  78.  
  79. /******************************************************************************/
  80.  
  81. #if defined(__SASC)
  82. void __tzset(void)
  83. {
  84.     static char time_zone_name[20] = "";
  85.  
  86.     /* This routine sets up the internal
  87.      * time zone variable according to
  88.      * the local settings.
  89.      */
  90.     if(time_zone_name[0] == '\0')
  91.     {
  92.         int hours_west = amiga_get_minutes_west() / 60;
  93.  
  94.         if(hours_west >= 0)
  95.             sprintf(time_zone_name,"GMT-%02d", hours_west);
  96.         else
  97.             sprintf(time_zone_name,"GMT+%02d",-hours_west);
  98.     }
  99.  
  100.     _TZ = time_zone_name;
  101. }
  102. #endif
  103.  
  104. /******************************************************************************/
  105.  
  106. time_t
  107. time(time_t *timeptr)
  108. {
  109.     time_t currentTime;
  110.     struct DateStamp ds;
  111.     LONG seconds;
  112.  
  113.   chkabort();
  114.  
  115.     DateStamp(&ds);
  116.  
  117.     seconds = ((ds.ds_Days * 24 * 60) + ds.ds_Minute) * 60 + (ds.ds_Tick / TICKS_PER_SECOND);
  118.  
  119.     currentTime = UNIX_TIME_OFFSET + seconds + 60 * amiga_get_minutes_west(); /* translate from local time to UTC */
  120.  
  121.     if(timeptr != NULL)
  122.         (*timeptr) = currentTime;
  123.  
  124.     return(currentTime);
  125. }
  126.  
  127. /******************************************************************************/
  128.  
  129. static struct tm *
  130. convert_time(ULONG seconds)
  131. {
  132.     static struct tm tm;
  133.  
  134.     struct ClockData clockData;
  135.     ULONG delta;
  136.  
  137.     Amiga2Date(seconds,&clockData);
  138.  
  139.     tm.tm_sec    = clockData.sec;
  140.     tm.tm_min    = clockData.min;
  141.     tm.tm_hour    = clockData.hour;
  142.     tm.tm_mday    = clockData.mday;
  143.     tm.tm_mon    = clockData.month - 1;
  144.     tm.tm_year    = clockData.year - 1900;
  145.     tm.tm_wday    = clockData.wday;
  146.     tm.tm_isdst    = 0;
  147.  
  148.     clockData.mday = 1;
  149.     clockData.month = 1;
  150.  
  151.     delta = Date2Amiga(&clockData);
  152.  
  153.     tm.tm_yday = (seconds - delta) / (24*60*60);
  154.  
  155.     return(&tm);
  156. }
  157.  
  158. char *
  159. asctime(const struct tm * tm)
  160. {
  161.     static char * month_names[12] =
  162.     {
  163.         "Jan","Feb","Mar","Apr",
  164.         "May","Jun","Jul","Aug",
  165.         "Sep","Oct","Nov","Dec"
  166.     };
  167.  
  168.     static char * day_names[7] =
  169.     {
  170.         "Sun","Mon","Tue","Wed",
  171.         "Thu","Fri","Sat"
  172.     };
  173.  
  174.     static char result_buffer[40];
  175.     char * month_name;
  176.     char * day_name;
  177.  
  178.     month_name = (0 <= tm->tm_mon && tm->tm_mon < 12) ? month_names[tm->tm_mon] : "---";
  179.     day_name = (0 <= tm->tm_wday && tm->tm_wday < 7) ? day_names[tm->tm_wday] : "---";
  180.  
  181.     sprintf(result_buffer,"%s %s %02d %02d:%02d:%02d %d\n",
  182.         day_name,
  183.         month_name,
  184.         tm->tm_mday,
  185.         tm->tm_hour,tm->tm_min,tm->tm_sec,
  186.         tm->tm_year + 1900);
  187.  
  188.     return(result_buffer);
  189. }
  190.  
  191. char *
  192. ctime(const time_t * t)
  193. {
  194.     char * result;
  195.  
  196.     result = asctime(localtime(t));
  197.  
  198.     return(result);
  199. }
  200.  
  201. struct tm *
  202. gmtime(const time_t *t)
  203. {
  204.     struct tm * result;
  205.     ULONG seconds;
  206.  
  207.     if((*t) < UNIX_TIME_OFFSET)
  208.         seconds = 0;
  209.     else
  210.         seconds = (*t) - UNIX_TIME_OFFSET;
  211.  
  212.     result = convert_time(seconds);
  213.  
  214.     return(result);
  215. }
  216.  
  217. struct tm *
  218. localtime(const time_t *t)
  219. {
  220.     struct tm * result;
  221.     ULONG seconds;
  222.  
  223.     if((*t) < (UNIX_TIME_OFFSET + 60 * amiga_get_minutes_west()))
  224.         seconds = 0;
  225.     else
  226.         seconds = (*t) - (UNIX_TIME_OFFSET + 60 * amiga_get_minutes_west()); /* translate from UTC to local time */
  227.  
  228.     result = convert_time(seconds);
  229.  
  230.     return(result);
  231. }
  232.